1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package com.google.common.xml;
18
19 import static com.google.common.escape.testing.EscaperAsserts.assertEscaping;
20 import static com.google.common.escape.testing.EscaperAsserts.assertUnescaped;
21
22 import com.google.common.annotations.GwtCompatible;
23 import com.google.common.escape.CharEscaper;
24
25 import junit.framework.TestCase;
26
27
28
29
30
31
32
33 @GwtCompatible
34 public class XmlEscapersTest extends TestCase {
35
36 public void testXmlContentEscaper() throws Exception {
37 CharEscaper xmlContentEscaper = (CharEscaper) XmlEscapers.xmlContentEscaper();
38 assertBasicXmlEscaper(xmlContentEscaper, false, false);
39
40 assertEquals("\"test\"", xmlContentEscaper.escape("\"test\""));
41 assertEquals("'test'", xmlContentEscaper.escape("'test'"));
42 }
43
44 public void testXmlAttributeEscaper() throws Exception {
45 CharEscaper xmlAttributeEscaper = (CharEscaper) XmlEscapers.xmlAttributeEscaper();
46 assertBasicXmlEscaper(xmlAttributeEscaper, true, true);
47
48 assertEquals(""test"", xmlAttributeEscaper.escape("\"test\""));
49 assertEquals("'test'", xmlAttributeEscaper.escape("\'test'"));
50
51 assertEquals("a"b<c>d&e"f'",
52 xmlAttributeEscaper.escape("a\"b<c>d&e\"f'"));
53
54 assertEquals("a	b
c
d", xmlAttributeEscaper.escape("a\tb\nc\rd"));
55 }
56
57
58 private void assertBasicXmlEscaper(CharEscaper xmlEscaper,
59 boolean shouldEscapeQuotes, boolean shouldEscapeWhitespaceChars) {
60
61 assertEquals("xxx", xmlEscaper.escape("xxx"));
62 assertEquals("test & test & test",
63 xmlEscaper.escape("test & test & test"));
64 assertEquals("test << 1", xmlEscaper.escape("test << 1"));
65 assertEquals("test >> 1", xmlEscaper.escape("test >> 1"));
66 assertEquals("<tab>", xmlEscaper.escape("<tab>"));
67
68
69 String s = "!@#$%^*()_+=-/?\\|]}[{,.;:" +
70 "abcdefghijklmnopqrstuvwxyz" +
71 "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
72 "1234567890";
73 assertEquals(s, xmlEscaper.escape(s));
74
75
76 for (char ch = 0; ch < 0x20; ch++) {
77 if (ch == '\t' || ch == '\n' || ch == '\r') {
78
79 if (shouldEscapeWhitespaceChars) {
80 assertEscaping(xmlEscaper, "&#x" + Integer.toHexString(ch).toUpperCase() + ";", ch);
81 } else {
82 assertUnescaped(xmlEscaper, ch);
83 }
84 } else {
85
86 assertEscaping(xmlEscaper, "\uFFFD", ch);
87 }
88 }
89
90
91 for (char ch = 0x20; ch <= 0xFFFD; ch++) {
92
93 if (ch == '&') {
94 assertEscaping(xmlEscaper, "&", ch);
95 } else if (ch == '<') {
96 assertEscaping(xmlEscaper, "<", ch);
97 } else if (ch == '>') {
98 assertEscaping(xmlEscaper, ">", ch);
99 } else if (shouldEscapeQuotes && ch == '\'') {
100 assertEscaping(xmlEscaper, "'", ch);
101 } else if (shouldEscapeQuotes && ch == '"') {
102 assertEscaping(xmlEscaper, """, ch);
103 } else {
104 String input = String.valueOf(ch);
105 String escaped = xmlEscaper.escape(input);
106 assertEquals(
107 "char 0x" + Integer.toString(ch, 16) + " should not be escaped",
108 input, escaped);
109 }
110 }
111
112
113 assertEscaping(xmlEscaper, "\uFFFD", '\uFFFE');
114 assertEscaping(xmlEscaper, "\uFFFD", '\uFFFF');
115
116 assertEquals("0xFFFE is forbidden and should be replaced during escaping",
117 "[\uFFFD]", xmlEscaper.escape("[\ufffe]"));
118 assertEquals("0xFFFF is forbidden and should be replaced during escaping",
119 "[\uFFFD]", xmlEscaper.escape("[\uffff]"));
120 }
121 }